package com.banmajio.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.banmajio.entity.User;
import com.banmajio.service.UserService;
import com.banmajio.util.RedisUtil;

@RestController
public class RedisController {

	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private UserService service;

	// 设置
	@RequestMapping("/setkey")
	public boolean setKey(String key, Object value) {
		return redisUtil.set(key, value);
	}

	@RequestMapping("/delkey")
	public void delKey(String key) {
		redisUtil.del(key);
	}

	@RequestMapping("/getUser")
	public User getUser(Long id) {
		return service.getUser(id);
	}

	@RequestMapping("/insertUser")
	public User insertUser(User user) {
		return service.insertUser(user);
	}

	@RequestMapping("/findUsers")
	public List<User> findUsers(String username, String note, Date createtime) {
		return service.findUsers(username, note, createtime);
	}

	@RequestMapping("/updateUser")
	public Map<String, Object> updateUser(User user) {
		user = service.updateUser(user);
		boolean flag = user != null;
		String msg = flag ? "更新成功！" : "更新失败！";
		HashMap<String, Object> resultMap = new HashMap<>();
		resultMap.put("success", flag);
		resultMap.put("msg", msg);
		return resultMap;
	}

	@RequestMapping("/deleteUser")
	public Map<String, Object> deleteUser(Long id) {
		int result = service.deleteUser(id);
		boolean flag = result == 1;
		String msg = flag ? "更新成功！" : "更新失败！";
		HashMap<String, Object> resultMap = new HashMap<>();
		resultMap.put("success", flag);
		resultMap.put("msg", msg);
		return resultMap;
	}
}
